﻿@using admin.Model
@{
    ViewData["Title"] = "UserList";
    Layout = "~/Views/Shared/_SubMenuLayout.cshtml";
    var customers = Model as List<UserOrder>;
}

<title>顾客列表</title>
<nav class="breadcrumb"><i class="Hui-iconfont">&#xe67f;</i> 首页 <span class="c-gray en">&gt;</span> 用户管理 <span class="c-gray en">&gt;</span> 顾客列表 <a class="btn btn-success radius r" style="line-height:1.6em;margin-top:3px" href="javascript:location.replace(location.href);" title="刷新"><i class="Hui-iconfont">&#xe68f;</i></a></nav>
<div class="page-container">
    <div class="cl pd-5 bg-1 bk-gray mt-20">
        <span class="l">
            <a href="javascript:;" onclick="user_batch_del('/Home/UserDelB')" class="btn btn-danger radius"><i class="Hui-iconfont">&#xe6e2;</i> 批量删除</a>
        </span>
        <span class="r">共有数据：<strong>@customers.Count</strong> 条</span>
    </div>
    <div class="mt-20">
        <table class="table table-border table-bordered table-hover table-bg table-sort">
            <thead>

                <tr class="text-c">
                    <th width="3%"><input type="checkbox" name="" value=""></th>
                    <th width="12%">顾客ID</th>
                    <th width="8%">顾客名</th>
                    <th width="4%">性别</th>
                    <th width="4%">年龄</th>
                    <th width="13%">电话</th>
                    <th width="15%">Mail</th>
                    <th width="">地址</th>
                    <th width="6%">总订单</th>
                    <th width="10%">操作</th>
                </tr>
            </thead>
            <tbody>
                @for (int i = 0; i < customers.Count; i++)
                {
                    <tr class="text-c">
                        <td><input type="checkbox" value="@customers[i].User.Cid" name="userChk"></td>
                        <td id="Cid">@customers[i].User.Cid</td>
                        <td><a title="查看用户信息" onclick="user_show('用户详细信息','/Home/UserShow','450','450','@customers[i].User.Cid')" href="javascript:;">@customers[i].User.Name </a></td>
                        <td>@customers[i].User.Sex</td>
                        <td>@customers[i].User.Age </td>
                        <td>@customers[i].User.Phone</td>
                        <td>@customers[i].User.Mail</td>
                        <td>@customers[i].User.Address</td>
                        <td><a title="查看订单信息" onclick="user_order('用户订单详细信息','/Home/UserOrderShow','@customers[i].User.Cid','@customers[i].Order.Count')" href="javascript:;">@customers[i].Order.Count</a></td>
                        <td class="td-manage">
                            <a title="编辑" href="javascript:;" onclick="user_edit('编辑用户信息','/Home/UserEdit','500','580','@customers[i].User.Cid')" class="ml-5" style="text-decoration:none"><i class="Hui-iconfont">&#xe6df;</i></a>
                            <a title="重置密码" href="javascript:;" onclick="user_reset_psd('/Home/UserResetPsd','@customers[i].User.Cid')" class="ml-5" style="text-decoration:none"><i class="Hui-iconfont">&#xe63f;</i></a>
                            <a title="删除" href="javascript:;" onclick="user_del(this,'/Home/UserDel','@customers[i].User.Cid')" class="ml-5" style="text-decoration:none"><i class="Hui-iconfont">&#xe6e2;</i></a>
                        </td>
                    </tr>
                }
            </tbody>
        </table>
    </div>
</div>

<script type="text/javascript">

    //查看用户总订单列表(待实现)
    function user_order(title, url, Cid, count) {
        //var url = url + '?id=' + Cid;
        if (count == "0") {
            layer.msg('该用户暂时没有订单哦！', { icon: 5, time: 2000 });
            return;
        }
        else {
            url = url + '?Cid=' + Cid;
            var index = layer.open({
                type: 2,
                title: title,
                content: url
            });
            layer.full(index);
        }
    }

    /*用户-编辑*/
    function user_edit(title, url, w, h, Cid) {
        url = url + '?Cid=' + Cid;
        //layer_show(title, url, w, h);
        //location.reload();

        layer.open({
            type: 2,
            area: [w + 'px', h + 'px'],
            fix: false, //不固定
            maxmin: true,
            shade: 0.4,
            title: title,
            content: url
        });
    }
    /*用户重置密码*/
    function user_reset_psd(url, Cid) {
        layer.confirm('确认要重置密码为"123456"吗？', function (index) {
            $.ajax({
                type: 'POST',
                url: url,
                data: {
                    "userid": Cid
                },
                dataType: "text",
                success: function (response) {
                    if (response == "ok") {
                        layer.msg('密码已重置!', { icon: 1, time: 3000 });
                    }
                    else
                        layer.msg('密码重置失败! 请稍后再试', { icon: 5, time: 3000 });
                    //location.reload();
                },
                error: function (response) {
                    layer.msg('密码重置失败! 请稍后再试', { icon: 5, time: 3000 });
                    //location.reload();
                },
            });
        });
    }

    /*用户-详情*/
    function user_show(title, url, w, h, Cid) {
        url = url + '?Cid=' + Cid;
        layer_show(title, url, w, h);
    }

    /*用户-删除*/
    function user_del(obj, url, Cid) {
        layer.confirm('确认要删除吗？', function (index) {
            $.ajax({
                type: 'POST',
                url: url,
                data: {
                    "userid": Cid
                },
                dataType: 'text',
                success: function (response) {
                    if (response == "ok") {
                        $(obj).parents("tr").remove();
                        layer.msg('已删除!', { icon: 1, time: 1000 });
                    }
                    else
                        layer.msg('删除失败! 所选用户存在未完成的订单', { icon: 5, time: 3000 });
                },
                error: function (response) {
                    layer.msg('删除失败! 所选用户存在未完成的订单', { icon: 5, time: 3000 });
                },
            });
        });
    }

    //用户批删除
    function user_batch_del(url) {
        var checkedNum = $("input[name='userChk']:checked").length;
        if (checkedNum == 0) {
            layer.msg('请选择至少一项！', { icon: 1, time: 2000 });
            return;
        }

        layer.confirm('确定要删除所选项目？', function (index) {
            var checkedList = new Array();
            $("input[name='userChk']:checked").each(function () {
                checkedList.push($(this).val());
            });
            $.ajax({
                type: "POST",
                url: url,
                data: { 'delitems': checkedList.toString() },
                success: function (result) {
                    if (result == "ok") {
                        $("input[name='userChk']:checked").each(function () {
                            $(this).parent("tr").remove();
                        });
                        $("[name ='userChk']:checkbox").attr("checked", false);
                        layer.msg('已删除!', { icon: 1, time: 3000 });
                        window.location.reload();
                    }
                    else
                        layer.msg('删除失败! 所选用户存在未完成的订单', { icon: 5, time: 3000 });
                },
                error: function (response) {
                    layer.msg('删除失败! 所选用户存在未完成的订单', { icon: 5, time: 3000 });
                },
            });
        });
    }
</script>